<?php
class Default_Model_Tour extends Zend_Db_Table_Abstract
    {
        
        protected $_name = 'tour';
        protected $_primary = 'tour_id';
        protected $_db;
        
        public function __construct(){
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listTour(){
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('status=1')
                            ->order('tour_id DESC');
            $result=$this->_db->fetchAll($sql);
            return $result;
        }
        
        public function listTourDefault() {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('status = 1')
                            ->order('tour_id DESC')
                            ->limit(4);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function listTourIndex($cat_id) {
            $cat_id = (int) $cat_id;
            if($cat_id == 0) {
                $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('status=1')
                            ->order('tour_id DESC');
            }
            else {
                $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('status=1')
                            ->where('cat_id = ?', $cat_id)
                            ->order('tour_id DESC');
            }
            $result=$this->_db->fetchAll($sql);
            return $result;
        }
        
        public function getTourById($id)
        {
            $sql=$this->_db->select()
                            ->from('tour')
                            ->where("tour_id=?", $id);
            $result=$this->_db->fetchRow($sql);
            return $result;
        }
        
        public function takeTourById($id)
        {
            $sql=$this->_db->select()
                            ->from('tour')
                            ->where("tour_id=$id");
            $result=$this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getTourById2($id)
        {
            $sql=$this->_db->select()
                            ->from('tour')
                            ->where("tour_id=?", $id);
            $result=$this->_db->fetchRow($sql);
            return $result;
        }
        
        public function convertToMysql($date)
        {
            $date = explode("/",$date);
            $date = array($date[2], $date[1], $date[0]);
            return $n_date=implode("-", $date);
        }
        
        
        public function searchTour($start_place,$finish_place,$start_date,$price)
        {
            $start_place = (int) $start_place;
            $finish_place = (int) $finish_place;
            
            $sql = "SELECT * FROM tour WHERE ";
            if($start_place !== 1) {
                $sql .= "start_place = " . $start_place . " AND ";
            }
            if($finish_place !== 1) {
                $sql .= "finish_place = " . $finish_place . " AND ";
            }
            if($start_date !== '') {
                $sql .= " start_date = '" . $start_date . "' AND ";
            }
            if($price==0)
            {
                $sql .= "adult_price < 2000000 ";
            }
            else if($price==1)
            {
                $sql .= "(adult_price >= 2000000 AND adult_price < 4000000) ";
            }
            else if($price==2)
            {
                $sql .= "(adult_price >= 4000000 AND adult_price < 6000000) ";
            }
            else if($price==3)
            {
                $sql .= "(adult_price >= 6000000 AND adult_price < 8000000) ";
            }
            else if($price==4)
            {
                $sql .= "(adult_price >= 8000000 AND adult_price < 10000000) ";
            }else if($price==5)
            {
                $sql .= "(adult_price >= 10000000 AND adult_price < 13000000) ";
            }
            else if($price==6)
            {
                $sql .= "(adult_price >= 13000000 AND adult_price < 16000000) ";
            }
            else if($price==7)
            {
                $sql .= "(adult_price >= 16000000 AND adult_price < 20000000) ";
            }
            else if($price==8)
            {
                $sql .= "adult_price >= 20000000";
            }
            $sql .= " AND status = 1";
            $result=$this->_db->fetchAll($sql);
            return $result;
        }
        
        public function recommend($tour_id)
        {
            
            $sql="SELECT tour_2 FROM similar WHERE tour_1=".$tour_id." AND tour_2 <> ".$tour_id." ORDER BY  point DESC limit 6";
           
            $tours=$this->_db->fetchAll($sql);
            $a=array();
            $i=0;
            
            foreach($tours as $tour){
                $sql2="SELECT * FROM tour WHERE tour_id=".$tour['tour_2'];
                $result=$this->_db->fetchRow($sql2);
                $a[$i]=$result;
                $i++;
            }
            
            return $a;
            
            // Tim ra nhung tour co ma tour nam trong kq tra ve tren
            
            
        }
        
        
        
        
        
    }
?>